//
//  TZzhuceinfo.m
//  XMLYFM
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 zzl. All rights reserved.
//

#import "TZzhuceinfo.h"
#import "CoreDataManager.h"
#import "User.h"
#import "TZzhuceMima.h"
@interface TZzhuceinfo ()
@property(nonatomic,retain)NSManagedObjectContext *managerObjectContext;
@property(nonatomic,retain)NSMutableArray *resultArr;//搜索结果
@end

@implementation TZzhuceinfo

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.nichengLabel.layer.borderWidth = 1;
    self.nichengLabel.layer.borderColor = [UIColor grayColor].CGColor;
    _managerObjectContext = [[CoreDataManager shareManager]managedObjectContext];
    NSLog(@"🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶%@",[[CoreDataManager shareManager]applicationDocumentsDirectory]);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)myAction:(UIButton *)sender {
  
    //查询请求
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    //获取要查询的实体的描述
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:_managerObjectContext];
    
    //对fetchRequest 设置查询的实体
    [fetchRequest setEntity:entity];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    
    //从数据库中搜索满足上面条件的元素
    NSArray *fetchedObjects = [_managerObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱%@",error);
    }
    
    _resultArr = [NSMutableArray arrayWithArray:fetchedObjects];
    NSMutableArray *TempArry = [NSMutableArray array];
    
    
    if (_resultArr.count == 0)
    {
        User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_managerObjectContext];
        
        
        
        TZzhuceMima *mima = [[TZzhuceMima alloc]init];
        

        user.name = self.nichengField.text;
        mima.mima = self.nichengField.text;
        mima.phoneNumber = self.phoneNumber;
        [[CoreDataManager shareManager]saveContext];
        
        [self.navigationController pushViewController:mima animated:YES];
        
        
    }
    else
    {
        for (User *user in _resultArr)
        {
            NSString *name = user.name;
            [TempArry addObject:name];
            
        }
        if ([TempArry containsObject:self.nichengField.text])
        {
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"该名字已经注册" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                [self.navigationController dismissViewControllerAnimated:YES completion:^{
                    
                    
                }];
                
            }];
            
            [alertC addAction:action];
        }
        else
        {
            User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_managerObjectContext];
            
            
            
            TZzhuceMima *mima = [[TZzhuceMima alloc]init];
            

            
            user.name = self.nichengField.text;
            mima.mima = self.nichengField.text;
            mima.phoneNumber = self.phoneNumber;
            [[CoreDataManager shareManager]saveContext];
            [self.navigationController pushViewController:mima animated:YES];

            
        }
        
        
        
    }
    

}
@end
